//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package api_common

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/bulletin_dao"
	"atomgit.com/openlinksaas/api-server/dao/chat_dao"
	"atomgit.com/openlinksaas/api-server/dao/comment_dao"
	"atomgit.com/openlinksaas/api-server/dao/project_dao"
	"atomgit.com/openlinksaas/api-server/dao/short_note_dao"
	"atomgit.com/openlinksaas/api-server/dao/user_dao"
	"atomgit.com/openlinksaas/api-server/dao/watch_dao"
)

func AddMember(ctx context.Context, nowTime int64, projectItem *project_dao.ProjectInfoDbItem, memberUserId, adminRoleId string) error {
	_, err := user_dao.UserInfoDao.Get(ctx, memberUserId)
	if err != nil {
		return err
	}
	exist, err := project_dao.MemberInfoDao.Exist(ctx, projectItem.ProjectId, memberUserId)
	if err != nil {
		return err
	}
	if !exist {
		roleId := projectItem.DefaultRoleId
		if projectItem.OwnerUserId == memberUserId && adminRoleId != "" {
			roleId = adminRoleId
		}

		err = project_dao.MemberInfoDao.Insert(ctx, &project_dao.MemberInfoDbItem{
			ProjectId:     projectItem.ProjectId,
			ProjectWeight: 0,
			MemberUserId:  memberUserId,
			RoleId:        roleId,
			CreateTime:    nowTime,
			UpdateTime:    nowTime,
		})
		if err != nil {
			return err
		}
	}

	//处理成员状态
	exist, err = project_dao.MemberStateDao.Exit(ctx, projectItem.ProjectId, memberUserId)
	if err != nil {
		return err
	}
	if !exist {
		err = project_dao.MemberStateDao.Insert(ctx, &project_dao.MemberStateDbItem{
			ProjectId:    projectItem.ProjectId,
			MemberUserId: memberUserId,
			TaskIssueState: project_dao.IssueStateDbItem{
				Dirty: false,
			},
			BugIssueState: project_dao.IssueStateDbItem{
				Dirty: false,
			},
		})
		if err != nil {
			return err
		}
	}

	//加入默认聊天组
	exist, err = chat_dao.GroupMemberDao.Exist(ctx, projectItem.ProjectId, projectItem.DefaultChatGroupId, memberUserId)
	if err != nil {
		return err
	}
	if !exist {
		err = chat_dao.GroupMemberDao.Insert(ctx, &chat_dao.MemberDbItem{
			ProjectId:   projectItem.ProjectId,
			ChatGroupId: projectItem.DefaultChatGroupId,
			UserId:      memberUserId,
			UnreadCount: 0,
			LastMsgTime: nowTime,
			JoinTime:    nowTime,
		})
		if err != nil {
			return err
		}
	}
	//删除join ack
	err = project_dao.AckJoinDao.Remove(ctx, projectItem.ProjectId, memberUserId)
	if err != nil {
		return err
	}
	return nil
}

func RemoveMember(ctx context.Context, projectId, memberUserId string) error {
	//删除成员
	err := project_dao.MemberInfoDao.Remove(ctx, projectId, memberUserId)
	if err != nil {
		return err
	}
	//删除相关的桌面便签
	err = short_note_dao.ShortNoteDao.RemoveByProject(ctx, memberUserId, projectId)
	if err != nil {
		return err
	}
	//删除公告相关数据
	err = bulletin_dao.UnReadInfoDao.RemoveByMember(ctx, projectId, memberUserId)
	if err != nil {
		return err
	}
	//取消所有关注
	err = watch_dao.MyWatchDao.RemoveByUser(ctx, projectId, memberUserId)
	if err != nil {
		return err
	}
	//删除未读评论信息
	err = comment_dao.UnReadDao.RemoveByUser(ctx, projectId, memberUserId)
	if err != nil {
		return err
	}
	//删除聊天组成员
	err = chat_dao.GroupMemberDao.RemoveByUser(ctx, projectId, memberUserId)
	if err != nil {
		return err
	}
	return nil
}
